/**
 *
 */
package com.flyco.modules.stm.service.impl;


import cn.hutool.core.bean.BeanUtil;

import com.flyco.modules.stm.entity.DistrictEntity;
import com.flyco.modules.stm.mapper.DistrictDao;
import com.flyco.modules.stm.service.DistrictService;
import com.flyco.modules.stm.util.PageUtil;
import com.flyco.modules.stm.vo.DistrictDto;
import com.flyco.modules.stm.vo.DistrictResult;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;


@Service
public class DistrictServiceImpl implements DistrictService {

    @Autowired
    private DistrictDao districtDao;

    @Override
    public DistrictDto selectOneById(Long id) {
        DistrictEntity districtEntity = districtDao.selectById(id);

        DistrictDto districtDto = new DistrictDto();
        BeanUtils.copyProperties(districtEntity, districtDto);
        return districtDto;
    }

    @Override
    public List<DistrictDto> selectOneLevelDistrict() {
        List<DistrictEntity> districtEntities = districtDao.selectOneLevelDistrict();

        List<DistrictDto> districtDtos = PageUtil.copyList(districtEntities, DistrictDto.class);
        return districtDtos;
    }

    @Override
    public List<DistrictDto> selectAllDistrict() {
        List<DistrictEntity> districtEntities = districtDao.selectAllDistrict();
        List<DistrictDto> districtDtos = PageUtil.copyList(districtEntities, DistrictDto.class);
        return districtDtos;
    }

    @Override
    public List<DistrictDto> selectChildDistrictData(Long parentDistrictId) {
        DistrictEntity districtEntity = districtDao.selectById(parentDistrictId);
        if (districtEntity != null) {
            return districtDao.selectChildDistrict(parentDistrictId);
        }
        return null;
    }

    @Override
    public List<DistrictResult> selectChildDistrictDataByParentId(Long parentDistrictId) {
        DistrictEntity districtEntity = districtDao.selectById(parentDistrictId);
        if (districtEntity != null) {
            List<DistrictDto> districtDtos = districtDao.selectChildDistrict(parentDistrictId);
            List<DistrictResult> districtResults = Lists.newArrayList();
            for (DistrictDto districtDto : districtDtos) {
                districtResults.add(BeanUtil.toBean(districtDto, DistrictResult.class));
            }

            return districtResults;
        }
        return Lists.newArrayList();
    }

    @Override
    public Integer deleteDistrictDataById(Long id) {
        return districtDao.deleteDistrictById(id);
    }

    @Override
    public Integer createDistrict(DistrictDto districtDto) {
        DistrictEntity districtEntity = districtDao.selectOneByCode(districtDto.getCode());
        if (null != districtEntity) {
            throw new RuntimeException("");
        }
        districtEntity = new DistrictEntity();
        BeanUtils.copyProperties(districtDto, districtEntity);

        districtDao.insert(districtEntity);

        // 如果存父区域字段存在值，更新该区域的序列号和名称
        Long parentDistrictId = districtEntity.getParentDistrictId();
        if (null != parentDistrictId) {
            DistrictEntity parentDistrictEntity = districtDao.selectById(parentDistrictId);
            if (null != parentDistrictEntity) {
                districtEntity.setParentDistrictName(parentDistrictEntity.getFullName());
                String parentDistrictSerialNo = parentDistrictEntity.getSerialNo();
                String parentDistrictSerialName = parentDistrictEntity.getSerialName();
                if (!StringUtils.isEmpty(parentDistrictSerialNo)) {
                    districtEntity.setSerialNo(parentDistrictSerialNo + "." + districtEntity.getId());
                }
                if (!StringUtils.isEmpty(parentDistrictSerialName)) {
                    districtEntity.setSerialName(parentDistrictSerialName + "->" + districtEntity.getShortName());
                }
            }
        } else {
            districtEntity.setParentDistrictId(0l);
            districtEntity.setParentDistrictName(" ");
            districtEntity.setSerialNo(districtEntity.getId().toString());
            districtEntity.setSerialName(districtEntity.getShortName());
        }
        return districtDao.updateById(districtEntity);
    }

    @Override
    public Integer modifyDistrict(DistrictDto districtDto) {
        DistrictEntity districtEntity = districtDao.selectById(districtDto.getId());
        if (districtEntity == null) {
            throw new RuntimeException("");
        }
        //检查唯一性
        String code = districtDto.getCode();
        if (!code.equals(districtEntity.getCode())) {
            if (null != districtDao.selectOneByCode(code)) {
                throw new RuntimeException("");
            }
        }
        //父区域被改，更新序列号和名称
        Boolean seriaChangeFalg = false;
        Long parentDistrictId = districtDto.getParentDistrictId();
        if (parentDistrictId == null) {
            districtDto.setParentDistrictId(0l);
            districtDto.setParentDistrictName(" ");
            districtDto.setSerialNo(districtEntity.getId().toString());
            districtDto.setSerialName(districtEntity.getShortName());
        } else {
            if (!parentDistrictId.equals(districtEntity.getParentDistrictId())) {
                seriaChangeFalg = true;
            }
        }
        if (seriaChangeFalg) {
            DistrictEntity parentDistrictEntity = districtDao.selectById(parentDistrictId);
            if (null != parentDistrictEntity) {
                districtDto.setParentDistrictName(parentDistrictEntity.getFullName());
                String parentDistrictSerialNo = parentDistrictEntity.getSerialNo();
                String parentDistrictSerialName = parentDistrictEntity.getSerialName();
                if (!StringUtils.isEmpty(parentDistrictSerialNo)) {
                    districtDto.setSerialNo(parentDistrictSerialNo + "." + districtEntity.getId());
                }
                if (!StringUtils.isEmpty(parentDistrictSerialName)) {
                    districtDto.setSerialName(parentDistrictSerialName + "->" + districtEntity.getShortName());
                }
            }
        }
        BeanUtils.copyProperties(districtDto, districtEntity);
        return districtDao.updateById(districtEntity);
    }

}
